import json
import os
from flask import Flask, jsonify,current_app
from py2neo import *
from django.shortcuts import render

# 连接数据库
graph = Graph('http://localhost:7474', user='neo4j', password='yhf20001116', name='neo4j')
def visualize_data():
    query = "MATCH (n) RETURN n"
    results = graph.run(query).data()
    rps = graph.relationships
    data = []
    links = []
    drugs = []  # 药品
    foods = []  # 食物
    checks = []  # 检查
    departments = []  # 科室
    producers = []  # 药品大类
    diseases = []  # 疾病
    symptoms = []  # 症状
    for result in results:
        print(result['n'].identity,type(result['n'].identity))
        node = {"name": result['n']['name'],'symbolSize': 50,'category': str(result['n'].labels)}
        data.append(node)
    for r in rps:
        # 取出开始节点的name
        print(rps[r].start_node.identity,type(rps[r].start_node.identity))
        source_id = rps[r].start_node.identity
        target_id = rps[r].end_node.identity
        source = str(rps[r].start_node['name'])
        # 取出结束节点的name
        target = str(rps[r].end_node['name'])
        # 取出开始节点的结束节点之间的关系
        name = str(type(rps[r]).__name__)
        link = {"source": source_id, "target": target_id, "type": name}
        links.append(link)
    neo4j_data =json.dumps({"data": data, "links": links},ensure_ascii=False)

    return neo4j_data

if __name__ == '__main__':
    print(123)
    neo4j_data = visualize_data()
